//
//  ProvinceViewController.m
//  LocomotiveGuards
//
//  Created by zwj on 15-11-19.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "ProvinceViewController.h"
#import "CityViewController.h"
#import "GDataXMLNode.h"
@interface ProvinceViewController () <UIPickerViewDataSource,UIPickerViewDelegate>
{
    UIPickerView *_pickerView;
    UIButton *_btnLast;
    UIButton *_btnNext;
    NSMutableArray *_provinceArray;
    NSMutableArray *_cityArray;
    NSMutableArray *_districtArray;
}
@end

@implementation ProvinceViewController

- (instancetype)init
{
    if (self = [super init]) {
        _provinceArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTitle:@"注册-身份验证"];
    [self basicXml];
    [self configView];
}

- (void)basicXml{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"province_data" ofType:@"xml"];
    if (path.length == 0) {
        NSLog(@"没有读到资源!");
        return;
    }
    
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];
    NSArray *pArray = [document nodesForXPath:@"/root/province" error:nil];
    
    for (GDataXMLElement *element in pArray) {
        NSString *provinceName = [[element attributeForName:@"name"] stringValue];
        NSArray *cArray = [element elementsForName:@"city"];
        _cityArray = [NSMutableArray array];
        for (GDataXMLElement *item in cArray) {
            NSString *cityName = [[item attributeForName:@"name"] stringValue];
            NSArray *dArray = [item elementsForName:@"district"];
            _districtArray = [NSMutableArray array];
            for (GDataXMLElement *sub in dArray) {
                NSString *distName = [[sub attributeForName:@"name"] stringValue];
                NSString *zipcode = [[sub attributeForName:@"zipcode"] stringValue];
                NSDictionary *dic = [NSDictionary dictionaryWithObjects:@[distName,zipcode] forKeys:@[@"dName",@"zipcode"]];
                [_districtArray addObject:dic];
            }
            
            NSDictionary *dic = [NSDictionary dictionaryWithObjects:@[cityName,_districtArray] forKeys:@[@"cName",@"distArr"]];
            [_cityArray addObject:dic];
        }
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjects:@[provinceName,_cityArray] forKeys:@[@"pName",@"cityArr"]];
        [_provinceArray addObject:dic];
    }
}

- (void)configView
{
    //所在省label
    UILabel *nameLabel = [self createLabelFrame:CGRectMake(30*W, 80*H, SCREEN_WIDTH-60*W, 30*H) title:@"所在省" font:[UIFont systemFontOfSize:26]];
    [self.view addSubview:nameLabel];
    
    _pickerView = [[UIPickerView alloc] init];
    _pickerView.center = CGPointMake(self.view.center.x, self.view.center.y+10*H);
    _pickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _pickerView.showsSelectionIndicator = YES;
    [_pickerView setBackgroundColor:[UIColor clearColor]];
    CALayer *viewLayer = _pickerView.layer;
    [viewLayer setBounds:CGRectMake(0, 0, SCREEN_WIDTH-80*W, SCREEN_HEIGHT-200*H)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [_pickerView selectRow:_provinceArray.count/2 inComponent:0 animated:YES];
    [self.view addSubview:_pickerView];
    
    //上一步按钮
    _btnLast = [self addLastBtnTarget:self action:@selector(btnLastClicked:)];
    [self enableButton:_btnLast];
    
    //下一步按钮
    _btnNext = [self addNextBtnTarget:self action:@selector(btnNextClicked:)];
    [self enableButton:_btnNext];
    
}

#pragma mark - 点击事件
//上一步，返回姓名填写页面
- (void)btnLastClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//下一步，进入新页面
- (void)btnNextClicked:(id)sender
{
    NSInteger num = [_pickerView selectedRowInComponent:0];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[_provinceArray[num] objectForKey:@"pName"] forKey:@"provinceName"];
    NSArray *selectProvince = [_provinceArray[num] objectForKey:@"cityArr"];
    CityViewController *cityCtrl = [CityViewController new];
    cityCtrl.cityArray = selectProvince;
    [self.navigationController pushViewController:cityCtrl animated:YES];
}

#pragma mark- UIPickerView代理函数
//行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _provinceArray.count;
}

//一共多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
     return 1;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *pickerLabel = (UILabel *)view;
    if (!pickerLabel) {
        pickerLabel = [UILabel new];
        pickerLabel.text = [_provinceArray[row] objectForKey:@"pName"];
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        pickerLabel.textColor = [UIColor whiteColor];
    }
    return pickerLabel;
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
